package com.seele.tigerwallet.ui.activtiy;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.seele.tigerwallet.R;
import com.seele.tigerwallet.api.APIException;
import com.seele.tigerwallet.api.APIListCallBack;
import com.seele.tigerwallet.api.APIModule;
import com.seele.tigerwallet.model.AssetsModel;
import com.seele.tigerwallet.model.MarketModel;
import com.seele.tigerwallet.ui.adapter.AddMoreAdapter;
import com.seele.tigerwallet.ui.adapter.MoreSreachAdapter;
import com.seele.tigerwallet.ui.base.TitleBaseActivity;
import com.seele.tigerwallet.ui.base.ToolbarBaseActivity;
import com.seele.tigerwallet.ui.callback.OnSelectListener;
import com.seele.tigerwallet.ui.event.AddMoreEvent;
import com.seele.tigerwallet.ui.utils.DividerItemDecoration;
import com.seele.tigerwallet.ui.utils.KeyBoardUtils;
import com.seele.tigerwallet.ui.views.AddMoreRecyclerView;
import com.seele.tigerwallet.ui.views.AddMoreSearchRecyclerView;
import com.seele.tigerwallet.ui.views.SearchEditext;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;

public class AddMoreActivity extends ToolbarBaseActivity {

    private AddMoreRecyclerView mRecyclerView;
    private AddMoreSearchRecyclerView mSearchRecyclerView;

    private long mSearchTime;
    private List<MarketModel> mBundleList;
    private String mkeyword;



    @Override
    public int getContainerViewId() {
        return R.layout.activity_add_more;

    }

    @Override
    protected void onStart() {

        super.onStart();

    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void initView() {
        setTitle(R.string.add_more);
        mRecyclerView = $f(R.id.add_more_recyclerView);
        mSearchRecyclerView = $f(R.id.search_recyclerView);
        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            mBundleList = (List<MarketModel>) bundle.getSerializable(AddMoreActivity.class.getSimpleName());
        }
        if  (null == mBundleList) {
            mBundleList = new ArrayList<>();
        }
        setNormalBar();
        setRecyclerView();



    }


    /**
     * 设置搜索的默认状态
     */
    private void setNormalSearchRecyclerView() {
        mRecyclerView.setVisibility(View.GONE);
        mSearchRecyclerView.setVisibility(View.VISIBLE);
        mSearchRecyclerView.setNormal();

    }


    /**
     * 设置基本的RecyclerView
     */
    public void setRecyclerView() {
        mSearchRecyclerView.setVisibility(View.GONE);
        mRecyclerView.setVisibility(View.VISIBLE);
        mRecyclerView.setBundleList(mBundleList, true);
        mRecyclerView.setListener(new OnSelectListener<MarketModel>() {
            @Override
            public void onSelect(List<MarketModel> marketModels) {
                mBundleList = marketModels;
            }
        });

    }

    @Override
    protected void onRightImg() {
        addSearchView(getResources().getString(R.string.sear_more_hint));
        //搜索View 设置一个空View,弹出键盘
        mSearchEditext.setFocusable(true);
        mSearchEditext.requestFocus();
        KeyBoardUtils.openKeybord(mSearchEditext,this);
        setNormalSearchRecyclerView();

    }

    @Override
    protected void onRightText() {

        KeyBoardUtils.closeKeybord(mSearchEditext,this);
        setTitle(R.string.add_more);
        setRightImg(R.drawable.search_select);
        setRecyclerView();
        setNormalBar();


    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        if (TextUtils.isEmpty(charSequence)) {
            sreachRecyclerView(null);
        } else if ((System.currentTimeMillis() - mSearchTime) > 300) {
            if (!TextUtils.isEmpty(charSequence)) {
                mkeyword = charSequence.toString();
                mSearchTime = System.currentTimeMillis();
                sreachRecyclerView(mkeyword);
            }
        }
    }

    /**
     * 设置搜索
     *
     * @param keyword
     */
    public void sreachRecyclerView(String keyword) {
        mRecyclerView.setVisibility(View.GONE);
        mSearchRecyclerView.setVisibility(View.VISIBLE);
        mSearchRecyclerView.setBundleList(mBundleList, keyword);
        mSearchRecyclerView.setListener(new OnSelectListener<MarketModel>() {
            @Override
            public void onSelect(List<MarketModel> marketModels) {
                mBundleList = marketModels;
            }
        });
    }




    @Override
    protected void onPaused() {
        AddMoreEvent addMoreEvent = new AddMoreEvent();
        addMoreEvent.mMarketModelList = mBundleList;
        EventBus.getDefault().post(addMoreEvent);
        super.onPaused();
    }
}
